package com.ch.android.lookforkotlinv2.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.ch.android.lookforkotlinv2.R
import com.ch.android.lookforkotlinv2.activity.WatchLiveActivity
import com.ch.android.lookforkotlinv2.databinding.LiveListItemBinding
import com.ch.android.networklib.data.LiveListItemData

/**
 * Author: conghao
 * Date: 2024/1/20 9:54
 * Description: LiveListAdapter
 */
class LiveListAdapter(private val items: List<LiveListItemData>) : RecyclerView.Adapter<BindingItemViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingItemViewHolder {
        val binding = DataBindingUtil.inflate<ViewDataBinding>(
            LayoutInflater.from(parent.context),
            R.layout.live_list_item,
            parent,
            false
        )
        return BindingItemViewHolder(binding.root)
    }

    override fun getItemCount() = items.size

    override fun onBindViewHolder(holder: BindingItemViewHolder, position: Int) {
        val binding: LiveListItemBinding = DataBindingUtil.getBinding(holder.itemView)!!
        binding.root.setOnClickListener { v ->
            Toast.makeText(v.context, "点击了第" + position + "条", Toast.LENGTH_SHORT).show()
            // 启动 WatchLiveActivity
            WatchLiveActivity.start(binding.root.context, items[position])
        }
        binding.item = items[position]
    }
}
